package spring.cloud.tasks.tasks_assign_manager.task_item_assign_event_handler;

import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import spring.cloud.tasks.tasks_assign_manager.service.TaskItemAssignService;
import spring.cloud.tasks.tasks_assign_manager.task_item_assign_event.ExecutorOnlineOrOfflineTaskItemAssignEvent;
import spring.cloud.tasks.tasks_assign_manager.task_item_assign_event.TaskItemAssignEvent;

@Slf4j
@Setter
@Getter
public class ExecutorOnlineOfflineTaskItemAssignEventHandler extends TaskItemAssignEventHandler {
    private TaskItemAssignService taskItemAssignService;


    public ExecutorOnlineOfflineTaskItemAssignEventHandler(TaskItemAssignService taskItemAssignService) {
        this.taskItemAssignService = taskItemAssignService;

    }

    @Override
    public void handle(TaskItemAssignEvent taskItemAssignEvent) throws Exception {
        try {
            if (taskItemAssignEvent instanceof ExecutorOnlineOrOfflineTaskItemAssignEvent) {
                ExecutorOnlineOrOfflineTaskItemAssignEvent event = (ExecutorOnlineOrOfflineTaskItemAssignEvent) taskItemAssignEvent;
                if (ExecutorOnlineOrOfflineTaskItemAssignEvent.type_executor_online.equals(event.getType())) {
                    taskItemAssignService.executorOnlineAllotTask(event.getExecutorId());
                } else if (ExecutorOnlineOrOfflineTaskItemAssignEvent.type_executor_offline.equals(event.getType())) {
                    taskItemAssignService.executorOfflineAllotTask(event.getExecutorId());
                }
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    }

}
